<template>
	<div class="gk-layout-container">
		<div class="gk-layout-header">
			<!--工具栏-->
			<s-query perms="core:stockDetail" @click="findPage" @addClick="handleAdd">
				<s-item v-model="filters.itemId" @input="findPage"></s-item>
				<el-form-item>
					<el-input v-model.trim="filters.boxcode" @keyup.enter.native="findPage(1)"
						placeholder="请输入盘码"></el-input>
				</el-form-item>
				<s-dict name="storeStatus" v-model="filters.storeStatus"> </s-dict>

				<s-date @dateChange="function (v) { filters.beginTime = v }" placeholder="时间" />

				<el-form-item>
					<el-input v-model="filters.shelfId" placeholder="请输入货架号"></el-input>
				</el-form-item>
			</s-query>
			<s-reflash reflash="true" excel="true" @findPage="findPage" @handlePreview="handlePreview"
				@exportExcel="exportExcel">
			</s-reflash>
		</div>
		<!--表格内容栏-->

		<div class="gk-layout-main">
			<my-table :data="pageResult.content" :loading="loading">
				<el-table-column :label="$t('action.desc')" width="50" header-align="center" align="center">
					<template slot-scope="scope"> {{ scope.$index + 1 }} </template>
				</el-table-column>
				<column prop="itemId" label="物料名称" header-align="left" align="left" width="450" :formatter="itemFilter" />
				<column prop="countDb" label="数量" :formatter="countFormat" />
				<column prop="status" label="状态" :formatter="selectionFormat1" />
				<column prop="boxCode" label="托盘码" />
				<column prop="rLocId" label="位置" />
				<column prop="changeTime" label="入库时间" width="140" />
				<el-table-column header-align="center" align="center" width="250" :label="$t('action.operation')">
					<template slot-scope="scope">
						<kt-button label="库外" perms="core:stockDetail:view" v-if="scope.row.status == 1"
							@click="pdaHidehandle(scope.$index, scope.row,2)" />
						<kt-button label="pda隐藏" perms="core:stockDetail:view" v-if="scope.row.status == 2&&scope.row.pid==0"
							@click="pdaHidehandle(scope.$index, scope.row,1)" />
						<kt-button label="查看" perms="core:stockDetail:view" v-if="scope.row.status != 2"
							@click="handleShow(scope.$index, scope.row, false)" />
						<kt-button label="编辑" v-if="scope.row.status == 2" perms="core:stockDetail:view"
							@click="handleShow(scope.$index, scope.row, true)" />

						<kt-button label="出库" v-if="scope.row.status == 9" perms="core:stockDetail:Out"
							@click="handleGenerate(scope.$index, scope.row)" />
						<kt-button label="拆分" v-if="scope.row.status == 19" perms="core:stockDetail:Out"
							@click="splitDet(scope.$index, scope.row)" />
						<kt-button label="打印" v-if="1 == 2" perms="core:stockDetail:view"
							@click="print(scope.$index, scope.row)" />

						<kt-delete-button v-if="scope.row.status == 2&&scope.row.pid==0" perms="core:stockDetail:view"
							@click="handleDelete(scope.$index, scope.row)" />
					</template>
				</el-table-column>
			</my-table>
		</div>
		<!--分页栏-->
		<div class="toolbar gk-layout-footer" style="padding:10px;">
			<el-pagination layout="total, prev, pager, next, jumper" @current-change="findPage"
				:current-page="filters.pageNum" :page-size="filters.pageSize" :total="pageResult.totalSize"
				style="float:right;">
			</el-pagination>
		</div>

		<core-stock-Det :detDialogVisible="dialogVisible" :editCount=operation :id=det.id @myClose="handleClose"
			@onSubmitForm="saveDet" />

	</div>
</template>

<script>
import KtButton from "@/views/Core/KtButton"
import MyTable from "@/views/Core/MyTable"
import Column from "@/views/Core/Column"
import SItem from "@/components/Kj/SItem"
import SDict from "@/components/Kj/SDict"
import SReflash from "@/components/Kj/SReflash"
import SQuery from "@/components/Kj/SQuery"
import SDate from "@/components/Kj/SDate"
import { format } from "@/utils/datetime"
import { addHtmlTd } from "@/utils/LodopFuncs"
import { getItemName, dictsFormat, getItem } from "@/utils/myUtil"
import KtDeleteButton from "@/views/Core/KtDeleteButton"
import { validatorNumber } from "@/utils/validate"
import CoreStockDet from "@/dialog/CoreStockDet"
export default {
	components: {
		KtButton,
		Column,
		MyTable,
		SQuery,
		CoreStockDet,
		KtDeleteButton,
		SItem,
		SDict,
		SDate,
		SReflash
	},
	data() {
		return {
			size: "mini",
			filters: {
				batchNo: '', pageNum: 1, pageSize: this.global.pageSize,
				pid: -1
			},
			operation: true,
			loading: false,
			dialogVisible: false,
			det: { id: 0, pid: 0, pkId: 0, status: 2 },
			pageResult: {}

		}
	},
	computed: {
		dataFormRules() {
			const dataFormRules = {
				itemId: [{ required: true, message: "请选择物料", trigger: 'blur' }],
				countDb: [{ required: true, message: "请输入数量", trigger: 'blur' }, { validator: validatorNumber, trigger: 'blur' }]
			};
			return dataFormRules;
		}
	},
	methods: {

		// 获取分页数据
		findPage: function (data) {
			if (data != -1) {
				data = data || 1
				this.filters.pageNum = data
			}
			this.$api.coreStockDet.findDetailPage(this.filters).then((res) => {
				this.pageResult = res.data
			})
		},


		handleAdd: function () {
			this.dialogVisible = true
			this.operation = true
			this.det = { id: 0, pid: 0, pkId: 0, status: 2 }
		},
		saveDet: function (d) {
			this.det.countDb = d.countDb
			this.$api.coreStockDet.saveStockDet(this.det).then((res) => {
				if (res.code == 200) {
					this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
					this.findPage(-1)
					this.handleClose()
				} else {
					this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
				}

			})
		},
		handleClose: function () {
			this.dialogVisible = false;
		},
		getKey: function (arg) {
			return this.$t(arg)
		},

		//导出的方法
		exportExcel(xlsExport) {
			this.filters.pageSize = -1

			this.$api.coreStockDet.findDetailPage(this.filters).then((res) => {
				const tHeader = ['物料编码', '物料名称', '物料规格', '数量', '状态', '托盘码', '位置', '入库时间'];
				const filterVal = ['itemId', 'name', 'gg', 'countDb', 'status', 'boxCode', 'rLocId', 'changeTime'];
				const list = res.data.content;
				for (var i = 0; i < list.length; i++) {
					var item = getItem(list[i].itemId);
					list[i].itemId = item.c;
					list[i].name = item.n;
					list[i].gg = item.g;
				}
				xlsExport(tHeader, filterVal, list, '库位库存导出');
			})
		},
		selectionFormat1: function (row, column, cellValue, index) {
			return dictsFormat("storeStatus", cellValue)
		},

		itemFilter: function (row, column, cellValue, index) {
			var item = getItemName(cellValue)
			return item;
		},

		countFormat: function (row, column, cellValue, index) {
			return cellValue + dictsFormat("units", row.unit)
		},

		// 显示打印界面
		handlePreview: function (myPrint) {

			this.$api.coreStockDet.printDetail(this.filters).then((res) => {
				var strList = res.data.info;
				var html = res.data.html;
				var detailHtml = "";
				for (let index = 0; index < strList.length; index++) {
					var detail = strList[index];
					detailHtml += "<tr>";
					detailHtml += addHtmlTd(getItemName(detail.itemId));
					detailHtml += addHtmlTd(detail.id);
					detailHtml += addHtmlTd(detail.stockStatus);
					detailHtml += addHtmlTd(detail.rLocId);
					detailHtml += "</tr>";
				}
				html = html.replace("{Content}", detailHtml);
				myPrint("库房资产", html)

			})

		},


		// 生成任务
		handleGenerate: function (index, row) {

			this.$confirm('确定出库吗？', '提示', {}).then(() => {

				this.$api.coreStockDet.detOutEngine(row).then((res) => {
					if (res.code == 200) {
						this.$message({ message: '操作成功', type: 'success' })
						this.findPage(-1)   //这里有bug
					} else {
						this.$message({ message: res.msg, type: 'error' })
					}
				})
			})
		},
	
		handleDelete: function (params, row) {
			this.rowDataForm = Object.assign({}, row);
			this.$api.coreStockDet.deleteStockDetail(this.rowDataForm).then((res) => {
				if (res.code == 200) {
					this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
				} else {
					this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
				}
				this.findPage(-1)
			})
		},
		pdaHidehandle: function (params, row,status) {
			this.rowDataForm = Object.assign({}, row);
			this.rowDataForm.status=status;
			this.$api.coreStockDet.pdaHideDet(this.rowDataForm).then((res) => {
				if (res.code == 200) {
					this.$message({ message: this.$t('action.operateSucess'), type: 'success' })
				} else {
					this.$message({ message: this.$t('action.operateFail') + res.msg, type: 'error' })
				}
				this.findPage(-1)
			})
		},
		print: function (params, row) {
			this.$api.print.print1d(row).then((res) => {
				this.$message({ message: "操作成功" + res.msg, type: 'success' })
			}).catch(e => {
				this.$message({ message: e.message, type: 'error' });
			})
		},

		// 显示编辑界面
		handleShow: function (params, row, op) {
			this.det.id = row.id
			this.det.doiDetId = row.doiDetId
			this.operation = op;
			this.dialogVisible = true


		},
		// 时间格式化
		dateFormat: function (row, column, cellValue, index) {
			return format(row[column.property])
		}
	},

	mounted() {

		this.size = this.global.gSize;
		this.findPage(1);
	}
}
</script>

<style scoped></style>